<?php

namespace app\award;

use app\models\NewLevel;
use app\models\order\Order;
use app\models\order\OrderItem;
use app\models\product\Product;
use app\models\product\ProductArea;
use app\models\SetProductCommission;
use app\models\SetUserUp;
use app\models\user\User;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserLevel;
use app\services\Chain;

class Award
{
    protected $order;
    protected $user;

    protected $set;

    public function __construct(Order $order)
    {
        $this->order = $order;
        $this->user = User::findOne(['id' => $this->order->user_id]);

        $first_order_item = OrderItem::find()
            ->where(['order_id' => $order->id])
            ->orderBy(['id' => SORT_ASC])
            ->limit(1)
            ->one();
        $product_id = $first_order_item->product_id;

        $this->set = SetUserUp::getLastOne();

        $is_bao = ProductArea::checkBao($product_id);
        $is_diff = ProductArea::checkDiff($product_id);


        // 新报单
        if ($is_bao) {
            $this->recommend();
        }

        // 差价区
        if ($is_diff) {
            $this->diff();
        }


    }


    // 推荐奖
    public function recommend()
    {
        $award_name = '推荐奖';
        $award_type = 11;

        $amount = $this->order->total_pay - $this->order->total_freight;

        $r = Chain::getReferrer($this->order->user_id, true);
        if ($r) {

            // vip1
            if ($amount == $this->set->vip1) {
                if ($r->level == UserLevel::$keys['vip2'] && $r->vip1_num > 0) {
                    $r->vip1_num -= 1;
                    $r->save();
                    $this->saveCommission($r->id, $amount, 20, '配额');
                } else {
                    $commission = 0;
                    if ($r->level == UserLevel::$keys['vip1']) {
                        $commission = $this->set->vip1_1;
                    }
                    if ($r->level == UserLevel::$keys['vip2']) {
                        $commission = $this->set->vip2_1;
                    }
                    $this->saveCommission($r->id, $commission, $award_type, $award_name);
                }
            }

            if ($amount == $this->set->vip2) {
                $commission = 0;
                if ($r->level == UserLevel::$keys['vip2']) {
                    $commission = $this->set->vip2_2;
                }
                $this->saveCommission($r->id, $commission, $award_type, $award_name);
            }
        }

    }

    // 补差价
    private function diff()
    {
        $award_name = '升级奖';
        $award_type = 12;

        $amount = $this->order->total_pay - $this->order->total_freight;

        $r = Chain::getReferrer($this->order->user_id, true);
        if ($r) {
            if ($amount == $this->set->diff) {
                $commission = 0;
                if ($r->level == UserLevel::$keys['vip2']) {
                    $commission = $this->set->vip2_1_2;
                }
                $this->saveCommission($r->id, $commission, $award_type, $award_name);
            }
        }
    }

    // 根据产品, 获取佣金
    private function getCommissionByProduct($id)
    {
        return SetProductCommission::find()
            ->where(['product_id' => $id])
            ->one();
    }



    // 保存佣金
    protected function saveCommission($user_id, $balance, $type, $name)
    {
        if ($balance >= 0.01) {
            $model = new UserCommission();
            $model->user_id = $user_id;
            $model->fans_id = $this->order->user_id;
            $model->order_id = $this->order->id;
            $model->balance = $balance;
            $model->status = 1;
            $model->award_name = $name;
            $model->award_type = $type;
            $model->save();

            $account = UserAccount::findOne(['user_id' => $user_id]);
            if ($account) {
                $account->balance += $balance;
                $account->save();
            }
        }
    }
}